import { useState, useEffect } from 'react'
function TimeFC() {
    const [time, setTime] = useState('')
    const formatTime = (time) => {
        return time < 10 ? `0${time}` : time  // 格式化时间，小于10的数字前面补0
    }
    useEffect(() => {
        // 开启定时器
        const timer = setInterval(() => {
            const date = new Date();
            const hours = formatTime(date.getHours()); // 获取当前小时
            const minutes = formatTime(date.getMinutes()); // 获取当前分钟
            const seconds = formatTime(date.getSeconds()); // 获取当前秒数
            setTime(`${hours}:${minutes}:${seconds}`) // HH:MM:SS
        }, 1000)
        return () => {
            // 清理定时器
            clearInterval(timer)
        }
    }, [])
    return <h1>{time}</h1>
}
export default TimeFC;